package Juego;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;
import Disparo.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.SwingUtilities;
import Inteligencia.*;
import Naves.NaveCaza;
import Naves.NaveXwing;
import Naves.NavesUsuario;
import java.awt.Point;
import TDALista.*;
import Naves.*;




/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/

public class Gui extends javax.swing.JFrame{
	
	private JButton BotonSalida;
	private JLabel Record;
	private JPanel PanelPrincipal;
	private JButton Nave1;
	private JButton jButton1;
	private JButton Bomba1;
	private JButton BotonSalir;
	private JLabel PuntosJugador;
	private JLabel FullPower;
	private JLabel PowerVida;
	private JLabel BombasPower;
	private JLabel DisparoPower;
	private JLabel PowerUP4;
	private JLabel PowerUP3;
	private JLabel PowerUP2;
	private JLabel PowerUP1;
	private JButton Bomba3;
	private JButton Bomba2;
	private JButton Nave3;
	private JButton Nave2;
	private JPanel PanelSecundario;
	private JLabel LabelNaves;
	private ImageIcon navexwing = new ImageIcon(getClass().getClassLoader().getResource("imagenes/xwing.png"));
	private ImageIcon bombasresto = new ImageIcon(getClass().getClassLoader().getResource("imagenes/bomba.png"));
	private ImageIcon powervida = new ImageIcon(getClass().getClassLoader().getResource("imagenes/vida.png"));
	private JLabel LabelVida;
	private ImageIcon powerfull = new ImageIcon(getClass().getClassLoader().getResource("imagenes/recarga.png"));
	private ImageIcon powerbomb = new ImageIcon(getClass().getClassLoader().getResource("imagenes/bombas.png"));
	private ImageIcon powerarma = new ImageIcon(getClass().getClassLoader().getResource("imagenes/disparos.png"));
	private ImageIcon espacio = new ImageIcon(getClass().getClassLoader().getResource("imagenes/espacio.gif"));
	private ImageIcon espacio2 = new ImageIcon(getClass().getClassLoader().getResource("imagenes/espacio2.png"));
	private ImageIcon botoncerrar = new ImageIcon(getClass().getClassLoader().getResource("imagenes/botonsalir.png"));
	private NavesUsuario jugador;
	protected PositionList<NavesEnemiga> listadeenemigos;
	protected PositionList<Bala> balasenemigas;
	protected PositionList<Bala> balasamigas;
	protected enemigosAI inteligenciaenemigos;
	protected balasAI inteligenciabalas;
	protected int ContadorPuntaje=0;
	String locacion = "audio/s1.mp3";

	
	
	/**
	* Auto-generated main method to display this JFrame
	*/
	
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Gui inst = new Gui();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public Gui() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			pack();
			
				
			this.setSize(800, 600);
			this.setResizable(false);
			this.setTitle("Star Wars");
			getContentPane().setLayout(null);
			{
				PanelPrincipal = new JPanel();
				getContentPane().add(PanelPrincipal);
				PanelPrincipal.setLayout(null);
				PanelPrincipal.setBounds(0, 0, 800, 500);
				PanelPrincipal.setBackground(new java.awt.Color(0,0,0));
				
				
			}
			{
				LabelNaves = new JLabel();
				PanelPrincipal.add(LabelNaves);
				LabelNaves.setBounds(0, 0, 800, 500);
			}
			{
				PanelSecundario = new JPanel();
				getContentPane().add(PanelSecundario);
				PanelSecundario.setBounds(0, 500, 800, 88);
				PanelSecundario.setLayout(null);
				PanelSecundario.setBackground(new java.awt.Color(0,0,0));
				{
					
					JLabel Nave1 = new JLabel(navexwing);
					PanelSecundario.add(Nave1);
					Nave1.setVisible(true);
					Nave1.setBounds(5, 15, 52, 57);
				}
				{
					JLabel Nave2 = new JLabel(navexwing);
					PanelSecundario.add(Nave2);
					Nave2.setVisible(true);
					Nave2.setBounds(56, 15, 52, 57);
				}
				{
					JLabel Nave3 = new JLabel(navexwing);
					PanelSecundario.add(Nave3);
					Nave3.setVisible(true);
					Nave3.setBounds(108, 15, 52, 57);
				}
				{
					JLabel Bomba1 = new JLabel(bombasresto);
					PanelSecundario.add(Bomba1);
					Bomba1.setVisible(true);
					Bomba1.setBounds(200, 15, 52, 57);
				}
				{
					JLabel Bomba2 = new JLabel(bombasresto);
					PanelSecundario.add(Bomba2);
					Bomba2.setVisible(true);
					Bomba2.setBounds(252, 15, 52, 57);
				}
				{
					JLabel Bomba3 = new JLabel(bombasresto);
					PanelSecundario.add(Bomba3);
					Bomba3.setVisible(true);
					Bomba3.setBounds(304, 15, 52, 57);
				}
				{
					PowerUP1 = new JLabel();
					PanelSecundario.add(PowerUP1);
					PowerUP1.setText("Vida Extra.");
					PowerUP1.setBounds(527, 17, 89, 16);
					PowerUP1.setForeground(new java.awt.Color(255,0,0));
				}
				{
					PowerUP2 = new JLabel();
					PanelSecundario.add(PowerUP2);
					PowerUP2.setText("Full Vida.");
					PowerUP2.setBounds(594, 17, 79, 16);
					PowerUP2.setForeground(new java.awt.Color(255,0,0));
				}
				{
					PowerUP3 = new JLabel();
					PanelSecundario.add(PowerUP3);
					PowerUP3.setText("Bomba Extra.");
					PowerUP3.setBounds(445, 17, 108, 16);
					PowerUP3.setForeground(new java.awt.Color(255,0,0));
				}
				{
					PowerUP4 = new JLabel();
					PanelSecundario.add(PowerUP4);
					PowerUP4.setText("1+Disparo.");
					PowerUP4.setBounds(378, 17, 97, 16);
					PowerUP4.setForeground(new java.awt.Color(255,0,0));
				}
				{
					DisparoPower = new JLabel(powerarma);
					PanelSecundario.add(DisparoPower);
					DisparoPower.setBounds(397, 39, 23, 22);
				}
				{
					BombasPower = new JLabel(powerbomb);
					PanelSecundario.add(BombasPower);
					BombasPower.setBounds(470, 39, 19, 22);
				}
				{
					PowerVida = new JLabel(powervida);
					PanelSecundario.add(PowerVida);
					PowerVida.setBounds(548, 39, 23, 22);
				}
				{
					FullPower = new JLabel(powerfull);
					PanelSecundario.add(FullPower);
					FullPower.setBounds(606, 38, 23, 24);
				}
				{
					Record = new JLabel();
					PanelSecundario.add(Record);
					Record.setText("Record  758379854");
					Record.setBounds(654, 17, 225, 16);
					Record.setForeground(new java.awt.Color(255,0,0));
				}
				{
					PuntosJugador = new JLabel();
					PanelSecundario.add(PuntosJugador);
					PuntosJugador.setText("Puntos: "+ContadorPuntaje);
					PuntosJugador.setBounds(654, 39, 195, 16);
					PuntosJugador.setForeground(new java.awt.Color(255,0,0));
					PuntosJugador.setFont(new java.awt.Font("Segoe UI",0,14));
				}
				{
					LabelVida = new JLabel();
					PanelSecundario.add(LabelVida);
					LabelVida.setText("Life Points: 500");
					LabelVida.setBounds(9, 6, 146, 16);
					LabelVida.setForeground(new java.awt.Color(255,0,0));
					LabelVida.setFont(new java.awt.Font("Segoe UI",0,14));
				}
				{
					BotonSalir = new JButton(botoncerrar);
					PanelSecundario.add(BotonSalir);
					BotonSalir.setBounds(767, 11, 27, 36);
					BotonSalir.setFont(new java.awt.Font("Segoe UI",0,14));
					BotonSalir.setForeground(new java.awt.Color(255,0,0));
					BotonSalir.setBackground(new java.awt.Color(0,0,0));
					BotonSalir.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
					BotonSalir.setFocusPainted(false);
					OyenteSalir oyente=new OyenteSalir();
					BotonSalir.addActionListener(oyente);
				}
				
				BotonSalir.setFocusable(false);
			}
			
			JLabel fondo1 = new JLabel(espacio);
			fondo1.setVisible(true);
			PanelPrincipal.add(fondo1);
			fondo1.setBounds(0, 0, 783, 600);
			
			JLabel fondo2 = new JLabel(espacio);
			fondo2.setVisible(true);
			PanelPrincipal.add(fondo2);
			fondo2.setBounds(770, 0, 26, 700);
			
			JLabel Fondo3 = new JLabel(espacio2);
			PanelSecundario.add(Fondo3);
			Fondo3.setBounds(2, 1, 793, 76);
			
			
			this.initgame();
			this.run();
		} catch (Exception e) {
			e.printStackTrace();
		}			
	}
	
	public NavesUsuario getUsuario(){
		return jugador;}
	
	public void initgame(){
		jugador = new NaveXwing(this);
		jugador.setBounds(400, 450, 40, 40);
		LabelNaves.add(jugador);

		
		listadeenemigos = new DoubleLinkedList<NavesEnemiga>();
		balasamigas=new DoubleLinkedList<Bala>();
		balasenemigas=new DoubleLinkedList<Bala>();
		inteligenciaenemigos=new enemigosAI(this);
		inteligenciabalas=new balasAI(this);
	}
	
	
	public void run() {
		Thread threadjugador=new Thread(jugador);
		threadjugador.start();
		mapa lvl=new mapa(this);
		lvl.start();
		inteligenciaenemigos.start();
		inteligenciabalas.start();
	}
	
	
	
	
	// METODOS QUE AGREGAGAN DIFERENTES TIPOS DE NAVE.
	public Iterable<Position<NavesEnemiga>> getListaEnemigos(){
		return listadeenemigos.positions();
	}
	
	public void InsertarCaza(NaveCaza nc){
		listadeenemigos.addLast(nc);
		int x=(int)(Math.random()*1000+1)%this.getWidth();
		Point pos=new Point(x,-50);
		LabelNaves.add(nc);
		nc.setLocation(pos);
		this.repaint();
	}
	
	public void InsertarDestructor(NaveDestructor nd){
		listadeenemigos.addLast(nd);
		LabelNaves.add(nd);
		this.repaint();
	}
	
	public void InsertarFederacion(NaveFederacion nf){
		listadeenemigos.addLast(nf);
		LabelNaves.add(nf);
		this.repaint();
	}
	
	public void SacarEnemigo(Position<NavesEnemiga> p){
		p.element().Explotar();
		ContadorPuntaje+=p.element().getPuntaje();
		PuntosJugador.setText("Puntos: "+ContadorPuntaje);
		try {listadeenemigos.remove(p);
		} catch (InvalidPositionException e) {}
	}
	
	
	//-------------------------------------------------------------
	//METODOS BALAS AMIGAS.
	
	public Iterable<Position<Bala>> getBalasAmigas(){
		return balasamigas.positions();
	}
	
	public void agregarBalaAmiga(Bala ba){
		this.LabelNaves.add(ba);
		balasamigas.addLast(ba);	
	}
	
	public void SacarBalaAmiga(Position<Bala> b){
		LabelNaves.remove(b.element());
		try {
			balasamigas.remove(b);
		} catch (InvalidPositionException e) {}
	}
	
	
	
	//-------------------------------------------------------------
	//METODOS BALAS ENEMIGAS.
	
	public Iterable<Position<Bala>> getBalasEnemigas(){
		return balasenemigas.positions();
	}
	
	public void agregarBalaEnemiga(Bala ba){
		this.LabelNaves.add(ba);
		balasenemigas.addLast(ba);
	}
	
	public void SacarBalaEnemiga(Position<Bala> b){
		LabelNaves.remove(b.element());
		LabelVida.setText("Life Points: "+jugador.getVida());
		try {
			balasenemigas.remove(b);
		} catch (InvalidPositionException e) {}
	}
	class OyenteSalir implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			System.exit(0);		
		}
	}	
}
